package cn.itcast.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet02 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、得到ServletConfig对象
	    ServletConfig config =  this.getServletConfig();
	    // 2、获取Servlet名称，对应的是web.xml中标签的的值，对应<servlet-name>的值
	    String servletName = config.getServletName();
	    System.out.println("servlet的名称" + servletName);
	    // 3、获取servlet初始化对象
	    String value = config.getInitParameter("encoding");
	    System.out.println("初始化参数的值：" + value);
	    // 4、获取初始化参数的名称
	    Enumeration<String> er = config.getInitParameterNames();
	    while (er.hasMoreElements()) {
	        // 获取初始化参数的名称
	        String name = er.nextElement();
	        // 根据初始化参数的名称，获取对应的值
	        String value1 = config.getInitParameter(name);
	        System.out.println("初始化参数的名称：" + name);
	        System.out.println("该名称对应的值：：" + value1);
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
